package com.yanqu.road.dao.impl.activity.lottery;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.lottery.UserActivityLotteryData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserActivityLotteryDataDaoImpl extends TempDataObjectDao<UserActivityLotteryData> {

    public static String getUserActivityLotteryDataSql = "select * from `t_u_activity_lottery_user_data` where `activityId`=? and `userId`=?";
    public UserActivityLotteryData getUserActivityLotteryData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserActivityLotteryDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<Integer, UserActivityLotteryData> getUserActivityLotteryData(List<Integer> activityIdList, long userId) {
        Map<Integer, UserActivityLotteryData> result = new ConcurrentHashMap<>();
        if(activityIdList.size() == 0){
            return result;
        }
        String sql = "select * from `t_u_activity_lottery_user_data` where `activityId` in("+ StringUtils.listToString(activityIdList, ",") +") and `userId`=?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityLotteryData data = parse(rs);
                result.put(data.getActivityId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateUserActivityLotteryDataSql = "update `t_u_activity_lottery_user_data` " +
            "set lotteryType=?,totalDrawTimes=?,commonDrawTimes=?,lastDrawItem=?,getSharedBox=?,getItemMap=?,noticeReadTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserActivityLotteryData userActivityLotteryData) {
        userActivityLotteryData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getLotteryType()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getCommonDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getLastDrawItem()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userActivityLotteryData.isGetSharedBox()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userActivityLotteryData.getGetItemMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityLotteryData.getNoticeReadTime()));

        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityLotteryData.getUserId()));
        if (getDb().executeNoneQuery(updateUserActivityLotteryDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserActivityLotteryDataSql = "insert into `t_u_activity_lottery_user_data`(activityId,lotteryType,userId,totalDrawTimes,commonDrawTimes,lastDrawItem,getSharedBox,getItemMap,noticeReadTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserActivityLotteryData userActivityLotteryData) {
        userActivityLotteryData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getLotteryType()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityLotteryData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getTotalDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getCommonDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityLotteryData.getLastDrawItem()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userActivityLotteryData.isGetSharedBox()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userActivityLotteryData.getGetItemMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityLotteryData.getNoticeReadTime()));
        if (getDb().executeNoneQuery(insertUserActivityLotteryDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserActivityLotteryData parseData(ResultSet rs) throws SQLException {
        UserActivityLotteryData data = new UserActivityLotteryData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setLotteryType(rs.getInt("lotteryType"));
        data.setTotalDrawTimes(rs.getInt("totalDrawTimes"));
        data.setCommonDrawTimes(rs.getInt("commonDrawTimes"));
        data.setLastDrawItem(rs.getInt("lastDrawItem"));
        data.setGetSharedBox(rs.getBoolean("getSharedBox"));
        data.setGetItemMap(JSON.parseObject(rs.getString("getItemMap"), new TypeReference<Map<Integer, Integer>>(){}));
        data.setNoticeReadTime(rs.getLong("noticeReadTime"));
        return data;
    }
}
